<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备编号" prop="deviceCode">
        <el-input
          v-model="queryParams.deviceCode"
          placeholder="请输入设备编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检定日期">
        <el-date-picker
          v-model="dateRange"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 240px"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="最新证书" prop="newly">
        <!-- <el-radio v-model="queryParams.newly" label="1"></el-radio> -->
        <el-checkbox v-model="queryParams.newly" true-label="1" false-label></el-checkbox>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['equipment:deviceCheckCert:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['equipment:deviceCheckCert:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['equipment:deviceCheckCert:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['equipment:deviceCheckCert:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-document-copy"
          size="mini"
          @click="handleClone"
          :disabled="single"
          v-hasPermi="['equipment:deviceCheckAsk:clone']"
        >克隆</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table
      @selection-change="handleSelectionChange"
      fit
      v-loading="loading"
      border
      :data="dataList"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column width="150" label="设备编号" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-link @click="handleDetail(scope.row)" type="primary">
            {{
            scope.row.deviceCode
            }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column width="150" label="设备名称" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-link @click="handleDetail(scope.row)" type="primary">
            {{
            scope.row.deviceName
            }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="检定/校准单位" prop="organLabel" />

      <el-table-column show-overflow-tooltip label="上次检定/校准日期" prop="checkDate" />
      <el-table-column show-overflow-tooltip width="150" label="校准/计量检定周期" prop="checkPeriod">
        <template slot-scope="scope">{{ scope.row.checkPeriod }}个月</template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="下次检定/校准日期" prop="effectDate" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="240"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            v-hasPermi="['equipment:deviceCheckCert:info']"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['equipment:deviceCheckCert:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['equipment:deviceCheckCert:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpload(scope.row)"
            v-hasPermi="['equipment:check:upload']"
          >上传</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog title="上传附件" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="附件信息">
          <FileUploadBase v-model="form.oss"></FileUploadBase>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDeviceCheckCert,
  updategdeviceCheckCert,
  delDeviceCheckCert,
  deviceCheckCertInfo
} from "@/api/device/deviceCheckCert.js";
import { deptTreeSelect } from "@/api/system/user.js";
export default {
  name: "deviceCheckCert",
  data() {
    return {
      dialogFormVisible: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      dataList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceId: undefined,
        checkDate: null,
        newly: "1"
      },
      // 日期范围
      dateRange: [],
      form: {},
      userList: [],
      deptOptions: []
    };
  },
  created() {
    const str = sessionStorage.getItem("queryParams");
    if (str) {
      this.queryParams = JSON.parse(str);
      if (this.queryParams.startEndDate) {
        this.dateRange.push(
          this.queryParams.startEndDate,
          this.queryParams.endEndDate
        );
      }
      setTimeout(() => {
        sessionStorage.removeItem("queryParams");
      }, 500);
    }
    this.getList();
    this.getDeptTree();
  },
  methods: {
    handleClone(row) {
      const configId = row.deviceCheckAskId || this.ids;
      this.$router.push(
        "./addDeviceCheckCert?isClone=true&id=" + configId + "&flag=1"
      );
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deviceCheckCertId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
      this.queryParams.ids = this.ids.toString();
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      sessionStorage.setItem("queryParams", JSON.stringify(this.queryParams));
      const configId = row.deviceCheckCertId || this.ids;
      this.$router.push("./addDeviceCheckCert?isEdit=true&id=" + configId);
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    handleExport() {
      this.download(
        "system/deviceCheckCert/export",
        {
          ...this.queryParams
        },
        `设备检测证书上传记录表_${this.parseTime(
          new Date().getTime(),
          "{y}-{m}-{d}"
        )}.xlsx`
      );
    },
    handleDetail(row) {
      sessionStorage.setItem("queryParams", JSON.stringify(this.queryParams));
      this.$router.push(
        "./addDeviceCheckCert?isInfo=true&id=" +
          row.deviceCheckCertId +
          "&isWx=" +
          this.$route.query.isWx
      );
    },
    /** 上传按钮操作 */
    handleUpload(row) {
      this.dialogFormVisible = true;
      this.form = row;
      const configId = row.deviceCheckCertId || this.ids;
      this.$router.push(
        "./addDeviceCheckCert?isEdit=true&id=" + configId + "&flag=" + 1
      );
      // deviceCheckCertInfo(row.deviceCheckCertId).then((response) => {
      //   this.form.oss = response.data.oss;
      //   console.log(this.formData.deviceId);
      // });
    },
    getinfo() {
      // this.listSelect();
    },
    submit() {
      updategdeviceCheckCert(this.form).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.resetForm();
        this.dialogFormVisible = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const configId = row.deviceCheckCertId || this.ids;
      let name = this.checkRow(
        configId,
        this.dataList,
        "deviceCheckCertId",
        "DELETE",
        "deviceCode"
      );
      this.$modal
        .confirm('是否确认删除设备编号为"' + name + '"的数据项？')
        .then(function() {
          return delDeviceCheckCert(configId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    handleAdd() {
      this.$router.push(
        "./addDeviceCheckCert?busId=" +
          this.$route.query.id +
          "&deviceId=" +
          this.$route.query.deviceId +
          "&isWx=" +
          this.$route.query.isWx +
          "&flag=" +
          1
      );
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      this.queryParams.deviceId = this.$route.query.deviceId;
      this.queryParams.busId = this.$route.query.id;

      listDeviceCheckCert(
        this.addDateRangeTime(
          this.queryParams,
          this.dateRange,
          "startEndDate",
          "endEndDate"
        )
      ).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__body {
  img {
    width: 100%;
  }
}
</style>
